package com.lu.servlet;

import com.lu.dao.impl.NewsDaoImpl;
import com.lu.entity.News;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class EditNewsServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html");

        PrintWriter out = resp.getWriter();

        int nid = Integer.parseInt(req.getParameter("nid"));
        int ntid = Integer.parseInt(req.getParameter("ntid"));
        String ntitle = req.getParameter("ntitle");
        String nauthor = req.getParameter("nauthor");
        String ncontent = req.getParameter("ncontent");
        String nsummary = req.getParameter("nsummary");

        News news = new News(nid,ntid,ntitle,nauthor,ncontent,nsummary);
        int result = new NewsDaoImpl().updateOneNews(news);
        if(result>0){
            out.print("<script>alert('修改成功!')</script>");
            out.print("<script>location.href='GetSomeNewsByPageServlet';</script>");
        }else{
            out.print("<script>alert('修改失败!')</script>");
            out.print("<script>history.back();</script>");
        }

        out.flush();
        out.close();
    }
}
